package tgc.edu.demo.jkxt.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import tgc.edu.demo.jkxt.custom.AjaxResult;
import tgc.edu.demo.jkxt.custom.BaseController;
import tgc.edu.demo.jkxt.entity.Shijuan;
import tgc.edu.demo.jkxt.form.ShijuanForm;
import tgc.edu.demo.jkxt.service.CourseService;
import tgc.edu.demo.jkxt.service.QuestionService;
import tgc.edu.demo.jkxt.service.ShijuanService;
import tgc.edu.demo.jkxt.entity.Course;
import tgc.edu.demo.jkxt.entity.Question;

@RestController
@RequestMapping(value = "/Shijuan")
@CrossOrigin
public class ShijuanController extends BaseController<Shijuan, Integer, ShijuanForm> {
	@Autowired
	private ShijuanService service;
	@Autowired
	private CourseService courseService;
	@Autowired
	private QuestionService questionService;
	private Date date;

	@Override
	public Object save(ShijuanForm form) {
		try {
			Shijuan model = new Shijuan();
			Integer id = form.getId();
			String time1 = form.getCreate_time();
			String time2 = form.getUpdate_time();

			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if (id != null) {
				model = service.findById(id);
			} 
			if (!time1.equals("")) {
				date = formatter.parse(time1);
				model.setCreate_time(date);
				if (!time2.equals("")) {
					date = formatter.parse(time2);
					model.setUpdate_time(date);
				}
			}
			model.setCoures(courseService.findById(form.getCourse_id()));
			model.setName(form.getName());
			model.setPanDuanFen(form.getPanDuanFen());
			BeanUtils.copyProperties(form, model, "id");
			service.save(model);
			return new AjaxResult("数据保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResult(false, "数据保存失败");
		}

	}

	@RequestMapping(value = "/findQuestion")
	public Object QindQuestionByShijuan(Integer shijuanid) {
		List<Question> questions = service.findQuestionsByShiJuanId(shijuanid);
		return new AjaxResult(questions);

	}
	@RequestMapping(value = "/findById")
	public Object findById(String id) {
		Integer shijuanId = Integer.valueOf(id);
		return new AjaxResult(service.findById(shijuanId));
	}

	@RequestMapping(value = "/chouti")
	public Object saveQuestions(Integer id) {
		try {
			
		Shijuan shijuan = service.findById(id);
		List<Question> danXuan = new ArrayList<Question>();
		List<Question> duoXuan = new ArrayList<Question>();
		List<Question> panDuan = new ArrayList<Question>();
		List<Question> questions = questionService.findQuestionByCourseId(shijuan.getCoures().getId());
		for (Question question : questions) {
			if (question.getType() == 1) {
				danXuan.add(question);
			} else if (question.getType() == 2) {
				duoXuan.add(question);
			} else {
				panDuan.add(question);
			}
		}
		List<Question> danXuanTi = createRandomList(danXuan, shijuan.getDanXuanNum());
		List<Question> duoXuanTi = createRandomList(duoXuan, shijuan.getDuoXuanNum());
		List<Question> panDuanTi = createRandomList(panDuan, shijuan.getPanDuanNum());
		List<List<Question>> list=new ArrayList<>();
		list.add(danXuanTi);
		list.add(duoXuanTi);
		list.add(panDuanTi);
		return new AjaxResult(list);
		}catch (Exception e) {
			return new AjaxResult(false,"数据错误！");
			// TODO: handle exception
		}
		
	}

	/**
	 * 从list中随机抽取元素
	 *
	 * @param list
	 * @param n
	 * @return void
	 * @throws @Title: createRandomList
	 * @Description: TODO
	 */
	private static List<Question> createRandomList(List<Question> list, Integer n) {
		// TODO Auto-generated method stub
		   // TODO Auto-generated method stub
        Map map = new HashMap();
        List listNew = new ArrayList();
        if (list.size() <= n) {
            return list;
        } else {
            while (map.size() < n) {
                int random = (int) (Math.random() * list.size());
                if (!map.containsKey(random)) {
                    map.put(random, "");
                    System.out.println(random + "===========" + list.get(random));
                    listNew.add(list.get(random));
                }
            }
            return listNew;
        }
	}
}
